/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * \file
 * \brief Simple tester for PNGDecoder.
 * \author Antti Maula <antti.maula@tkk.fi>
 */

//*****************************************************************************
#include "PNGDecoder.hpp"
#include "owndebug.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************

int main(int argc, char *argv[])
{
  int i;
  char outfile[255];
  CPNGDecoder decoder;

  debugInit();
  debugSetGlobalDebugLvl(3);



  if (argc < 2) {
    printf("Usage: %s <input_jpeg> ...\n",argv[0]);
    printf("For example, try running '%s *'\n\n",argv[0]);
  } else {
    char *data = NULL;
    struct stat s;

    // As long as there are parameters...
    for(i=1; i < argc; i++) {
      if (stat(argv[i], &s) == 0) {
	
        // Check that file is regular.
        if (! (s.st_mode & S_IFREG)) {
          printf("Skipping entry '%s' - Not a regular file\n", argv[i]);
          continue;
        }

	// Allocate mem for whole PNG.
	data = new char [s.st_size];
	memset(data,0x00,s.st_size);

	// Read the file in memory
	FILE *in = fopen(argv[i],"rb");
	if (in) {
	  const int x = fread(data, s.st_size, 1, in);
          assert(x == 1);
	  fclose(in);

	  // Set decoder ptr.
	  decoder.SetPNGDataPtr(data, s.st_size);

          // Decode
          decoder.Decode();
	  
	  // Write out the file
	  sprintf(outfile,"%s.rgb", argv[i]);
	  const int rgbW = decoder.GetImageWidth();
	  const int rgbH = decoder.GetImageHeight(); 
	  const char *rgbData = decoder.GetRGBDataPtr();
	  const int rgbDataSize = decoder.GetRGBDataSize();
	  if (rgbData) {
	    printf("Converted PNG '%s' (size: %u bytes, w:%d, h:%d) to RGB-file '%s' (Size %d bytes)\n",
		   argv[i], (unsigned)s.st_size, rgbW, rgbH, outfile, rgbDataSize);
	    FILE *out = fopen(outfile,"wb");
	    if (out) {
	      fwrite(decoder.GetRGBDataPtr(), 
		     decoder.GetRGBDataSize(), 
		     1, out);
	      fclose(out);
	    } else {
	      printf("Failed to open output file '%s'!\n", outfile);

	    }
	  } else {
	    printf("FAILED to convert PNG '%s' to RGB-file '%s'\n",
		   argv[i], outfile);
	    
	  }
	} else {
	  printf("FAILED to fopen() input file '%s' (%s)!\n", argv[i], strerror(errno));
	}
	
	delete[] data;
      } else {
	printf("FAILED to stat() input file '%s' (%s)!\n", argv[i], strerror(errno));
      }
    }
  }
  

  return 0;
}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
